/**
 * 
 */
package com.es.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.es.business.lead.ILeadManagement;
import com.es.exception.business.ESException;
import com.es.model.Lead;
import com.es.util.ProjectConstant;




/**
 * @author Bushra Nazir
 *
 */
public class LeadController extends BaseController{

	private static final Logger log = Logger.getLogger(LeadController.class);

	private String tempVar;
	private String tempVarDL;
	private String tempVarVL;
	private String tempVarVLD;
	private String tempVarMLD;

	private Lead lead;

	private List<SelectItem> leadSourceSelectItems;
	private List<SelectItem>  industrySelectItems;
	private List<SelectItem> leadStatusSelectItems;
	private List<SelectItem> leadRatingSelectItems;
	private List<SelectItem> deadLeadSelectItems;

	private List<Lead> deadLeadList;
	private List<Lead> leadList;

	private ILeadManagement leadManagementImpl;

	private String navigationIdentifier = null; 

	public List<String> selectedDeadLeads = new ArrayList<String>();

	private Integer selectedLeadIdToView;
	private Integer selectedLeadIdToModify;
	private Integer selectedLeadIdToConvert;

	private Lead selectedLeadToView;
	private Lead selectedLeadToModify;
	private Lead selectedLeadToConvert;


	public void initView(){

		leadSourceSelectItems = UtilityController.getLeadSourceSelectItems();
		industrySelectItems = UtilityController.getIndustrySelectItems();
		leadStatusSelectItems = UtilityController.getLeadStatusSelectItems();
		leadRatingSelectItems = UtilityController.getLeadRatingSelectItems();

	}

	public void initForDeadLeadView(){

		log.info(" inside initForDeadLeadView() ");
		FacesContext fc = FacesContext.getCurrentInstance();
		String msg =null;

		try {
			deadLeadList = leadManagementImpl.getDeadLeadList();
		} catch (ESException e) {		
			msg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "RETRIEVING_DEAD_LEADS_FAILED_MSG", null);
			addGlobalFacesMessage(fc, msg, null, FacesMessage.SEVERITY_ERROR);

		}

		if (deadLeadList.size()==0){
			msg = "there are no leads with current status as DEAD.. ";
			addGlobalFacesMessage(fc, msg, null, FacesMessage.SEVERITY_INFO);

		}

		deadLeadSelectItems = UtilityController.getDeadLeadSelectItems(deadLeadList);
	}

	public void initForLeadDetailView(){

		log.info(" inside initForLeadDetailView() ");
		FacesContext fc = FacesContext.getCurrentInstance();
		String msg =null;

		try{

			leadList = leadManagementImpl.getLeadList();
		}catch (ESException e) {		
			msg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEADS_RETRIEVAL_FAILED_MSG", null);
			addGlobalFacesMessage(fc, msg, null, FacesMessage.SEVERITY_ERROR);

		}

	}


	public void initForViewLeadDetail(){

		log.info(" inside initForViewLeadDetail() ");

		selectedLeadToView = getSelectedLeadById(selectedLeadIdToView);

	}


	public void initForModifyLeadDetail(){

		log.info(" inside initForModifyLeadDetail() ");
		initView();
		selectedLeadToModify = getSelectedLeadById(selectedLeadIdToModify);


	}


	public Lead getSelectedLeadById(Integer selectedLeadIdToView) {

		log.info(" inside getSelectedLeadById() ");
		Lead selectedLead = null;

		try {
			selectedLead = leadManagementImpl.getLead(selectedLeadIdToView);

		} catch (ESException e) {

			String errMsg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEAD_RETRIEVAL_FAILED_MSG",null);
			FacesContext fc = FacesContext.getCurrentInstance();
			addGlobalFacesMessage(fc, errMsg, null, FacesMessage.SEVERITY_ERROR);
		}

		return selectedLead;

	}

	/**
	 *@author Bushra Nazir
	 *@param null
	 *@return String  
	 */
	public String createLead(){

		try {
			leadManagementImpl.createLead(lead);
		} catch (ESException e) {

			String errMsg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEAD_CREATION_FAILED_MSG",null);
			FacesContext fc = FacesContext.getCurrentInstance();
			addGlobalFacesMessage(fc, errMsg, null, FacesMessage.SEVERITY_ERROR);
			return null;
		}


		navigationIdentifier= ProjectConstant.SALES_AGENT_DASHBOARD_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;
	}



	/**
	 *@author Bushra Nazir
	 *@param null
	 * @return String 
	 */
	public String navigateToSalesAgentDashboard(){

		navigationIdentifier= ProjectConstant.SALES_AGENT_DASHBOARD_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}



	/**
	 *@author Bushra Nazir
	 *@param null
	 * @return String
	 */
	public String deleteDeadLeads(){

		log.info(" inside deleteDeadLeads() ");
		log.info( "leads to be deleted ::" + selectedDeadLeads.size());  // zero 

		navigationIdentifier = ProjectConstant.SALES_AGENT_DASHBOARD_NAVIGATION_IDENTIFIER;

		if(selectedDeadLeads.size()!=0){

			log.info(" size>0 ");

			try {
				leadManagementImpl.deleteDeadLeads(selectedDeadLeads);
			} catch (ESException e) {

				String errMsg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEADS_DELETION_FAILED_MSG",null);
				FacesContext fc = FacesContext.getCurrentInstance();
				addGlobalFacesMessage(fc, errMsg, null, FacesMessage.SEVERITY_ERROR);
			}

		}

		return navigationIdentifier;

	}


	public String navigateToViewLeadDetail(){

		log.info("inside navigateToViewLeadDetail() ");

		selectedLeadIdToView = Integer.parseInt(getParamValueFromRequest(ProjectConstant.SELECTED_LEADID_TO_VIEW_PARAM_NAME));
		setSelectedLeadIdToView( selectedLeadIdToView );

		navigationIdentifier= ProjectConstant.VIEW_LEAD_DETAIL_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}


	public String navigateToModifyLeadDetail(){

		log.info("inside navigateToModifyLeadDetail() ");

		selectedLeadIdToModify = Integer.parseInt(getParamValueFromRequest(ProjectConstant.SELECTED_LEADID_TO_MODIFY_PARAM_NAME));
		setSelectedLeadIdToModify( selectedLeadIdToModify );

		navigationIdentifier= ProjectConstant.MODIFY_LEAD_DETAIL_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}



	public String modifyLead(){

		log.info("inside modifyLead() ");

		try{

			leadManagementImpl.modifyLead(selectedLeadToModify);

		}catch (ESException e) {

			String errMsg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEAD_MODIFICATION_FAILED_MSG",null);
			FacesContext fc = FacesContext.getCurrentInstance();
			addGlobalFacesMessage(fc, errMsg, null, FacesMessage.SEVERITY_ERROR);
		}


		navigationIdentifier= ProjectConstant.VIEW_LEADS_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}


	public String navigateToViewLeads(){

		log.info("inside navigateToViewLeads() ");

		navigationIdentifier= ProjectConstant.VIEW_LEADS_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}


	
	public String navigateToConvertLeadToOpportunity(){

		log.info("inside navigateToConvertLeadToOpportunity() ");

		selectedLeadIdToConvert = Integer.parseInt(getParamValueFromRequest(ProjectConstant.SELECTED_LEADID_TO_CONVERT_PARAM_NAME));
		setSelectedLeadIdToConvert( selectedLeadIdToConvert );
		
		selectedLeadToConvert = getSelectedLeadById(selectedLeadIdToConvert);
		
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.getExternalContext().getRequestMap().put(ProjectConstant.LEAD_TO_BE_CONVERTED_SESSION_KEY, selectedLeadToConvert);

		navigationIdentifier= ProjectConstant.LEAD_QUALIFICATION_QUERIES_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}







	/*public void getCurrentUser{



	}*/





	public Lead getLead() {
		return lead;
	}

	public void setLead(Lead lead) {
		this.lead = lead;
	}

	public String getTempVar() {

		initView();
		//	getCurrentUser();
		return tempVar;
	}

	public void setTempVar(String tempVar) {
		this.tempVar = tempVar;
	}

	public List<SelectItem> getLeadSourceSelectItems() {
		return leadSourceSelectItems;
	}

	public void setLeadSourceSelectItems(List<SelectItem> leadSourceSelectItems) {
		this.leadSourceSelectItems = leadSourceSelectItems;
	}

	public List<SelectItem> getIndustrySelectItems() {
		return industrySelectItems;
	}

	public void setIndustrySelectItems(List<SelectItem> industrySelectItems) {
		this.industrySelectItems = industrySelectItems;
	}

	public List<SelectItem> getLeadStatusSelectItems() {
		return leadStatusSelectItems;
	}

	public void setLeadStatusSelectItems(List<SelectItem> leadStatusSelectItems) {
		this.leadStatusSelectItems = leadStatusSelectItems;
	}

	public List<SelectItem> getLeadRatingSelectItems() {
		return leadRatingSelectItems;
	}

	public void setLeadRatingSelectItems(List<SelectItem> leadRatingSelectItems) {
		this.leadRatingSelectItems = leadRatingSelectItems;
	}

	public ILeadManagement getLeadManagementImpl() {
		return leadManagementImpl;
	}

	public void setLeadManagementImpl(ILeadManagement leadManagementImpl) {
		this.leadManagementImpl = leadManagementImpl;
	}



	public String getTempVarDL() {
		initForDeadLeadView();
		return tempVarDL;
	}



	public void setTempVarDL(String tempVarDL) {
		this.tempVarDL = tempVarDL;
	}

	public List<SelectItem> getDeadLeadSelectItems() {
		return deadLeadSelectItems;
	}

	public void setDeadLeadSelectItems(List<SelectItem> deadLeadSelectItems) {
		this.deadLeadSelectItems = deadLeadSelectItems;
	}

	public void setDeadLeadList(List<Lead> deadLeadList) {
		this.deadLeadList = deadLeadList;
	}

	public List<String> getSelectedDeadLeads() {
		return selectedDeadLeads;
	}

	public void setSelectedDeadLeads(List<String> selectedDeadLeads) {
		log.info("selected");
		this.selectedDeadLeads = selectedDeadLeads;
	}

	public String getTempVarVL() {
		initForLeadDetailView();
		return tempVarVL;
	}

	public void setTempVarVL(String tempVarVL) {
		this.tempVarVL = tempVarVL;
	}

	public List<Lead> getLeadList() {
		return leadList;
	}

	public void setLeadList(List<Lead> leadList) {
		this.leadList = leadList;
	}

	public void setSelectedLeadIdToView(Integer selectedLeadIdToView) {
		this.selectedLeadIdToView = selectedLeadIdToView;
	}

	public void setSelectedLeadIdToModify(Integer selectedLeadIdToModify) {
		this.selectedLeadIdToModify = selectedLeadIdToModify;
	}

	public Integer getSelectedLeadIdToView() {
		return selectedLeadIdToView;
	}

	public Integer getSelectedLeadIdToModify() {
		return selectedLeadIdToModify;
	}

	public String getTempVarVLD() {	
		initForViewLeadDetail();
		return tempVarVLD;
	}

	public void setTempVarVLD(String tempVarVLD) {
		this.tempVarVLD = tempVarVLD;
	}

	public Lead getSelectedLeadToView() {
		return selectedLeadToView;
	}

	public void setSelectedLeadToView(Lead selectedLeadToView) {
		this.selectedLeadToView = selectedLeadToView;
	}

	public Lead getSelectedLeadToModify() {
		return selectedLeadToModify;
	}

	public void setSelectedLeadToModify(Lead selectedLeadToModify) {
		this.selectedLeadToModify = selectedLeadToModify;
	}

	public String getTempVarMLD() {
		initForModifyLeadDetail();
		return tempVarMLD;
	}

	public void setTempVarMLD(String tempVarMLD) {
		this.tempVarMLD = tempVarMLD;
	}

	public Integer getSelectedLeadIdToConvert() {
		return selectedLeadIdToConvert;
	}

	public void setSelectedLeadIdToConvert(Integer selectedLeadIdToConvert) {
		this.selectedLeadIdToConvert = selectedLeadIdToConvert;
	}

	public Lead getSelectedLeadToConvert() {
		return selectedLeadToConvert;
	}

	public void setSelectedLeadToConvert(Lead selectedLeadToConvert) {
		this.selectedLeadToConvert = selectedLeadToConvert;
	}



}
